// pages/coupon/get.js
var app = getApp()

Page({

  data: {
    ticketid: 0,
    isDisable: false,
    openid:'',
    status:0,
    detail:[]
  },

  onLoad: function (options) {
    this.checkOpenid();
    console.log(options);
    var scene = decodeURIComponent(options.scene);
    var params = scene.split(',');
    this.setData({
      ticketid : params[0]
    });
    this.getCoupon(params[0]);
  },

  checkOpenid: function(){
    var openid = wx.getStorageSync('userOpenid');
    if(openid == ''){
      this.getUserOpenid();
    }else{
      this.setData({
        openid: openid
      });
    }
  },
  // 获取openid
  getUserOpenid: function(){
    var that=this;
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求
          app.util.request({
            url: 'entry/wxapp/getUserOpenid',
            data: {
              code: res.code
            },
            cachetime: 30,
            success: function (res) {
              var openid = res.data.data.openid;
              wx.setStorageSync('userOpenid', openid);
              that.setData({
                openid: openid
              })
            }
          })
        }
      }
    });
  },
  draw: function(){
    var that = this;
    var openid = that.data.openid;
      if(!openid){
        that.getUserOpenid();
        var openid = that.data.openid;
      }
      that.setData({
        isDisable: true
      });
      app.util.request({
        url: 'entry/wxapp/draw',
        data: {
           openid: that.data.openid,
           ticketid: that.data.ticketid
        },
        cachetime: 0,
        success: function (res) {
          if(res.data.errno == 0){
            wx.showToast({
              title: '领取成功！',
              icon: 'success',
              duration: 2000,
              success: function () {          
                setTimeout(function () {
                  wx.redirectTo({
                    url: 'index'
                  })
                }, 2000) //延迟时间
              }
            });
          }
        },
        fail: function (res) {
          if(res.data.errno == 1){
            wx.showModal({
              content: res.data.message,
              showCancel: false,
              success: function (res) {          
                if(res.confirm){
                  wx.redirectTo({
                    url: '/pages/coupon/index',
                  })
                }
              }
            });
          }else{
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 2000
            });
          }
          return false;
        }
      })
  },
  checkStatus:function(){
    var that = this;
    app.util.request({
      url: 'entry/wxapp/checkStatus',
      data: {
         ticketid: that.data.ticketid
      },
      cachetime: 0,
      success: function (res) {
        that.setData({
          status: res.data.data
        });
        if(that.data.status==1){
          wx.showModal({
            title: '提示',
            content: '当前优惠券已被他人领取',
            confirmText: '知道了',
            showCancel: false,
            success (res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: '/pages/coupon/index'
                })
              }
            }
          })
        }else{
          that.draw();
        }
      }
    })
  },
  getCoupon: function(ticketid){
    var that = this;
    app.util.request({
      url: 'entry/wxapp/getCouponBytid',
      data: {
         ticketid: ticketid
      },
      cachetime: 0,
      success: function (res) {
        if(res.data.errno == 0){
          that.setData({
            detail: res.data.data,
            status: res.data.data.status
          });
        }
      },
      fail: function (res) {
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 2000
        });
        return false;
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})